package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;

    @RequestMapping("/r1")
    public String registry(String name, String password) {
        //⽤⼾注册
        //开始事务
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);
        userService.registryUser(name, password);

        //提交事务
//        dataSourceTransactionManager.commit(transaction);

        //回滚事务
        dataSourceTransactionManager.rollback(transaction);
        return "注册成功";
    }
}
